package controller

import (
	apiserver "apedge/apiserver"
	"apedge/dto"

	"github.com/gin-gonic/gin"
)

func ApedgeInferenceRouter(r *gin.Engine) {
	group := r.Group("/api/v1/apedge/inference")

	group.GET("", dto.Wrapper(GetApedgeInferenceList))
	group.GET("/:serviceId", dto.Wrapper(GetApedgeInferenceDetail))
	group.POST("", dto.Wrapper(CreateApedgeInference))
	group.DELETE("/:serviceId", dto.Wrapper(DelApedgeInference))

	// service - node 表
	// 每个service下的节点列表 进行相关操作
	group.GET("/serviceNode/:serviceId", dto.Wrapper(GetServiceNodeList))
	group.PATCH("/serviceNode", dto.Wrapper(OpApedgeInference))
	group.DELETE("/serviceNode/:serviceNodeId", dto.Wrapper(DeleteServiceNode))

	// 调用指南
	group.GET("/predict_info/:serviceId", dto.Wrapper(PredictInfo))
}

func CreateApedgeInference(c *gin.Context) error {
	var err error
	var req dto.CreateApedgeInferenceReq
	var rsp dto.CreateApedgeInferenceRsp

	err = c.ShouldBindJSON(&req)
	if err != nil {
		return dto.ParameterError(c, err.Error())
	}
	userInfo, errRsp := PreHandler(c, &req)
	if errRsp != nil {
		return errRsp
	}
	id, err := apiserver.CreateApedgeInference(req, userInfo)
	if err != nil {
		logger.Errorln(err)
		return dto.ErrorToCodeRsp(c, err)
	}

	rsp = dto.CreateApedgeInferenceRsp{
		ServiceId: id,
	}
	return dto.SuccessResp(c, rsp)
}

func GetApedgeInferenceList(c *gin.Context) error {
	var req dto.GetApedgeInferenceListReq
	var rsp dto.GetApedgeInferenceListRsp
	var err error

	var list []dto.OutApedgeInferenceItem
	var count int64

	err = c.ShouldBindQuery(&req)
	if err != nil {
		return dto.ParameterError(c, err.Error())
	}
	userInfo, errRsp := PreHandler(c, &req)
	if errRsp != nil {
		return errRsp
	}

	list, count, err = apiserver.GetApedgeInferenceList(req, userInfo)
	if err != nil {
		return dto.ErrorToCodeRsp(c, err)
	}

	rsp = dto.GetApedgeInferenceListRsp{
		Items: list,
		Total: count,
	}
	return dto.SuccessResp(c, rsp)
}

func GetApedgeInferenceDetail(c *gin.Context) error {
	var err error
	var req dto.GetApedgeInferenceDetailReq
	var rsp dto.GetApedgeInferenceDetailRsp

	err = c.ShouldBindUri(&req)
	if err != nil {
		return dto.ParameterError(c, err.Error())
	}
	_, errRsp := PreHandler(c, &req)
	if errRsp != nil {
		return errRsp
	}
	out, err := apiserver.GetApedgeInferenceDetail(req.ServiceId)
	if err != nil {
		return dto.ErrorToCodeRsp(c, err)
	}

	rsp = dto.GetApedgeInferenceDetailRsp{
		OutApedgeInferenceItem: *out,
	}
	return dto.SuccessResp(c, rsp)
}

func DelApedgeInference(c *gin.Context) error {
	var err error
	var req dto.DelApedgeInferenceReq
	var rsp dto.DelApedgeInferenceRsp

	err = c.ShouldBindUri(&req)
	if err != nil {
		return dto.ParameterError(c, err.Error())
	}

	_, errRsp := PreHandler(c, &req)
	if errRsp != nil {
		return errRsp
	}

	err = apiserver.DelApedgeInference(req.ServiceId)
	if err != nil {
		return dto.ErrorToCodeRsp(c, err)
	}

	return dto.SuccessResp(c, rsp)
}

func OpApedgeInference(c *gin.Context) error {
	var err error
	var req dto.OpInferenceTaskReq
	var rsp dto.OpInferenceTaskRsp

	err = c.ShouldBindJSON(&req)
	if err != nil {
		return dto.ParameterError(c, err.Error())
	}
	userInfo, errRsp := PreHandler(c, &req)
	if errRsp != nil {
		return errRsp
	}

	err = apiserver.OpInferenceTask(req, userInfo)
	if err != nil {
		return dto.ErrorToCodeRsp(c, err)
	}

	return dto.SuccessResp(c, rsp)
}

func GetServiceNodeList(c *gin.Context) error {
	var err error
	var req dto.GetServiceNodeReq
	var rsp dto.GetServiceNodeRsp

	err = c.ShouldBindUri(&req)
	if err != nil {
		return dto.ParameterError(c, err.Error())
	}
	_, errRsp := PreHandler(c, &req)
	if errRsp != nil {
		return errRsp
	}
	out, err := apiserver.GetServiceNodeList(req)
	if err != nil {
		return dto.ErrorToCodeRsp(c, err)
	}

	rsp = dto.GetServiceNodeRsp{
		Items: out,
		Total: len(out),
	}
	return dto.SuccessResp(c, rsp)
}

func DeleteServiceNode(c *gin.Context) error {
	var err error
	var req dto.DelServiceNodeReq
	var rsp dto.DelServiceNodeRsp

	err = c.ShouldBindUri(&req)
	if err != nil {
		return dto.ParameterError(c, err.Error())
	}

	_, errRsp := PreHandler(c, &req)
	if errRsp != nil {
		return errRsp
	}

	err = apiserver.DeleteServiceNode(req.ServiceNodeId)
	if err != nil {
		return dto.ErrorToCodeRsp(c, err)
	}

	return dto.SuccessResp(c, rsp)
}

func PredictInfo(c *gin.Context) error {
	var req dto.PredictInfoReq
	var rsp *dto.PredictInfoRsp

	err := c.ShouldBindUri(&req)
	if err != nil {
		return dto.ParameterError(c, err.Error())
	}

	_, errRsp := PreHandler(c, &req)
	if errRsp != nil {
		return errRsp
	}

	rsp, err = apiserver.PredictInfo(req.ServiceId)
	if err != nil {
		return dto.ErrorToCodeRsp(c, err)
	}

	return dto.SuccessResp(c, rsp)
}
